package cn.iocoder.yudao.module.book.controller.app.forum;


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.book.controller.app.bookcase.vo.AppBooksRespVO;
import cn.iocoder.yudao.module.book.controller.app.booksShell.vo.AppCollectionsRespVO;
import cn.iocoder.yudao.module.book.service.books.BooksService;
import cn.iocoder.yudao.module.book.service.bookshelf.BookshelfService;
import cn.iocoder.yudao.module.book.service.collections.CollectionsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Tag(name = "APP - 资源论坛")
@RestController
@RequestMapping("/api/user/bbs")
//测试完成
public class AppForumController {

    @Resource
    private CollectionsService collectionsService;

    @Resource
    private BooksService booksService;

    @Resource
    private BookshelfService bookshelfService;

    @GetMapping("/bookList")
    @Operation(summary = "获取绘本集列表")
    public CommonResult<List<AppCollectionsRespVO>> getCategories() {
        return  CommonResult.success(collectionsService.selectAll());
    }

    @GetMapping("/addBook")
    @Operation(summary = "添加绘本")
    public CommonResult<Boolean> addBook(@Valid @RequestParam("id") Integer id, @Valid @RequestParam("userId") Integer userId) {
        AppBooksRespVO book=booksService.getByBookId(id,null);
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        return  CommonResult.success(bookshelfService.addBook(book.getBookId(),book.getCollectionId(),userId,time));
    }

    @GetMapping("/addSet")
    @Operation(summary = "添加绘本集")
    public CommonResult<Boolean> addAll(@Valid @RequestParam("userId") Integer userId, @Valid @RequestParam("collectionId") Integer collectionId) {
        List<AppBooksRespVO> books=booksService.getByCollectionId(collectionId);
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

        for(AppBooksRespVO book:books){
            bookshelfService.addBook(book.getBookId(),collectionId,userId,time);
        }
        return  CommonResult.success(true);
    }

}
